<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="emp.name">
  <input type="text" placeholder="请输入员工工资" v-model="emp.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="emp.position">
  <button @click="addEmp">点我添加</button>
  <br>
  <table border="1">
    <tr>
      <th>员工编号</th>
      <th>员工姓名</th>
      <th>员工工资</th>
      <th>员工岗位</th>
    </tr>
    <tr v-for="(e,index) in arr">
      <td>{{index+1}}</td>
      <td>{{e.name}}</td>
      <td>{{e.salary}}</td>
      <td>{{e.position}}</td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";
//定义响应式对象用来保存收集到的员工数据
const emp = ref({name: '', salary: '', position: ''})
//定义数组来保存表格中的多个员工数据
const arr = ref([])
//定义添加员工的方法
const addEmp = () => {
  //判断是否有未输入的数据
  if (emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.position.trim()=='') {
    alert('请输入完整的员工信息')
     return
  }
  //将收集到的员工数据存入数组中
  arr.value.push(emp.value)
  //注意：用完的数据一定要清空！
  emp.value = {name: '', salary: '', position: ''}
  console.log(arr.value)
}
</script>

<style scoped>

</style>